from osv import osv, fields
from osv.orm import intersect
import time

#Red Invoice
class kderp_suplier_vat_invoice(osv.osv):
    _name="kderp.supplier.vat.invoice"
    _description="KDERP Supplier VAT Invoice Information"
    _rec_name = 'vat_rop_no'
    
    def _new_supplier(self, cr, uid, context={}):
        supplier_id = False
        if context:
            if 'supplier_id' in context:
                supplier_id = context['supplier_id']
        return supplier_id
    
    def _new_currency(self, cr, uid, context={}):
        if not context:
            context={}
        currency_id = context.get("currency",False)
        return currency_id
    
    def _new_subtotal_amount(self, cr, uid, context={}):
        this_time=0
        if context:
            if 'payment_amount' in context:
                this_time=context['payment_amount']
        return this_time
    
    def _new_total_amount_amount(self, cr, uid, context={}):
        this_time=0
        if not context:
            context={}
        this_time=context.get('payment_amount',0.0) + context.get('tax_amount',0.0)
        return this_time
    
    def _new_tax_per(self, cr, uid, context={}):
        this_time=0
        if context:
            if 'tax_per' in context:
                this_time=context['tax_per']
        return this_time
    
    def _new_tax_amount(self, cr, uid, context={}):
        this_time=0
        if context:
            if 'tax_amount' in context:
                this_time=context['tax_amount']
        return this_time
    
    _order = 'date desc'
    
    _columns={
             'supplier_id':fields.many2one("res.partner",'Supplier',ondelete="restrict"),
             'name':fields.char('Invoice No.',size=32,required=True),
             'vat_rop_no':fields.char('R.O.P. No.',size=16),
             'date':fields.date('Date',required=True),
             'currency_id':fields.many2one('kderp.currency','Currency'),
             'subtotal':fields.float('Sub-Total'),
             'tax_percent':fields.float('VAT (%)'),
             'amount_tax':fields.float('Tax'),
             'total_amount':fields.float('Total'),
             'kderp_payment_to_supplier_ids':fields.many2many('kdvn.request.of.payment','kderp_rop_supplier_vat_invoice', 'kderp_supplier_vat_invoice_id', 'rop_id', 'Payment'),
             }
    
    _sql_constraints = [('ps_code_supplier_unique', 'unique (name,supplier_id)', 'The VAT Invoice no of a supplier must be unique !')]
    
    _defaults={
               'subtotal':_new_subtotal_amount,
               'total_amount':_new_total_amount_amount,
               'tax_percent':_new_tax_per,
               'amount_tax':_new_tax_amount,
               'supplier_id':_new_supplier,
               'currency_id':_new_currency,
               #'total_amount':,
               }
    
    def _onchange_totalamount(self,cr,uid,ids,total_amount,type,currency_id,date,kdvn_check,rate,equivalent_vnd):
        result={}
         # Bien dung de check xem goi tu form hay tu view hay code
        if type=='currency_id':            
            curr_id = currency_id
            rate=0
            if curr_id:
                if not date:
                    date=time.strftime('%Y-%m-%d')
                else:                            
                    date=self.pool.get('kdvn.common.function').check_date(date)
                                
                cr.execute("SELECT fncompute(%s,get_default(),'%s')" %(curr_id, date))
                
                kdvn_po_rate_obj = cr.fetchone()
                if kdvn_po_rate_obj:
                    rate = kdvn_po_rate_obj[0]
            result={'rate':rate,'equivalent_company_curr':total_amount*rate}
        elif total_amount==0:
            result={'rate':1}
        elif type=='total_amount':
            result={'equivalent_company_curr':total_amount*rate}
        elif type=='equivalent_company_curr':
            new_rate = 0.0
            if total_amount>0:
                new_rate = equivalent_vnd / total_amount
            result={'rate':new_rate}
        elif type=='rate':
            result={'equivalent_company_curr':rate*total_amount}
        
        if kdvn_check:        
            return {'value':result}
        else:
            return result

    def _on_changevalue(self,cr,uid,ids,amount,tax_percent,amount_tax):
        if not amount_tax:
            amount_tax=amount*(tax_percent or 0.0)/100.0
            total_amount=(amount or 0.0)+ (amount_tax or 0.0)
            result={'amount_tax':amount_tax,'total_amount':total_amount}
        else:
            total_amount=(amount or 0)+(amount_tax or 0)
            result={'amount_tax':amount_tax,'total_amount':total_amount}
        return {'value':result}
    
kderp_suplier_vat_invoice()